<?php

class CoolnessTableRow {
	
	private $rank;
	private $country;
	private $neededDivisions;
	private $day1;
	private $day1_color;
	private $day1_percentage;
	private $week1;
	private $week1_color;
	private $week1_percentage;
	private $week8;
	private $week8_color;
	private $week8_percentage;
	private $week16;
	private $week16_color;
	private $week16_percentage;
	private $week32;
	private $week32_color;
	private $week32_percentage;
	private $visited = false;
	private $hosted = false;
	private $color;
	
	public function __construct($rank, $country, $day1, $week1, $week8, $week16, $week32, $away, $home) {
			$this->setRank($rank);
			$this->setCountry($country);
			$this->setNeededDivisions($this->calculateNeededDivisions($country->getActiveTeams() + $country->getWaitingUsers()));
			$this->setDay1($day1[$country->getCountryID()]);
			$this->setWeek1($week1[$country->getCountryID()]);
			$this->setWeek8($week8[$country->getCountryID()]);
			$this->setWeek16($week16[$country->getCountryID()]);
			$this->setWeek32($week32[$country->getCountryID()]);
			$this->setVisited($away[$country->getCountryID()]);
			$this->setHosted($home[$country->getCountryID()]);
			$this->setColor($this->hasVisited(), $this->hasHosted());
	}
	
	public function getRank() {
		return $this->rank;
	}
	
	public function setRank($rank) {
		$this->rank = $rank;
	}
	
	public function getCountry() {
		return $this->country;
	}
	
	public function setCountry($country) {
		$this->country = $country;
	}
	
	public function getNeededDivisions() {
		return $this->neededDivisions;
	}
	
	public function setNeededDivisions($neededDivisions) {
		$this->neededDivisions = $neededDivisions;
	}
	
	public function getDay1() {
		return $this->day1;
	}
	
	public function setDay1($day1) {
		if ($day1 instanceof Country) {
			$this->day1 = $this->getCountry()->getActiveTeams()-$day1->getActiveTeams();
			$this->setDay1_Percentage($this->getCountry()->getActiveTeams(), $day1->getActiveTeams());
			$this->setDay1_Color($this->getDay1_Percentage());
		}
	}
	
	public function getDay1_Percentage() {
		return $this->day1_percentage;
	}
	
	private function setDay1_Percentage($current, $day1) {
		$this->day1_percentage = $this->calculatePercentage($current, $day1);
	}
	
	public function getDay1_Color() {
		return $this->day1_color;
	}
	
	private function setDay1_Color($percentage) {
		$this->day1_color = $this->calculateColor($percentage);
	}
	
	public function getWeek1() {
		return $this->week1;
	}
	
	public function setWeek1($week1) {
		if ($week1 instanceof Country) {
			$this->week1 = $this->getCountry()->getActiveTeams()-$week1->getActiveTeams();
			$this->setWeek1_Percentage($this->getCountry()->getActiveTeams(), $week1->getActiveTeams());
			$this->setWeek1_Color($this->getWeek1_Percentage());
		}
	}
	
	public function getWeek1_Percentage() {
		return $this->week1_percentage;
	}
	
	private function setWeek1_Percentage($current, $week1) {
		$this->week1_percentage = $this->calculatePercentage($current, $week1);
	}
	
	public function getWeek1_Color() {
		return $this->week1_color;
	}
	
	private function setWeek1_Color($percentage) {
		$this->week1_color = $this->calculateColor($percentage);
	}
	
	public function getWeek8() {
		return $this->week8;
	}
	
	public function setWeek8($week8) {
		if ($week8 instanceof Country) {
			$this->week8 = $this->getCountry()->getActiveTeams()-$week8->getActiveTeams();
			$this->setWeek8_Percentage($this->getCountry()->getActiveTeams(), $week8->getActiveTeams());
			$this->setWeek8_Color($this->getWeek8_Percentage());
		}
	}
	
	public function getWeek8_Percentage() {
		return $this->week8_percentage;
	}
	
	private function setWeek8_Percentage($current, $week8) {
		$this->week8_percentage = $this->calculatePercentage($current, $week8);
	}
	
	public function getWeek8_Color() {
		return $this->week8_color;
	}
	
	private function setWeek8_Color($percentage) {
		$this->week8_color = $this->calculateColor($percentage);
	}
	
	public function getWeek16() {
		return $this->week16;
	}
	
	public function setWeek16($week16) {
		if ($week16 instanceof Country) {
			$this->week16 = $this->getCountry()->getActiveTeams()-$week16->getActiveTeams();
			$this->setWeek16_Percentage($this->getCountry()->getActiveTeams(), $week16->getActiveTeams());
			$this->setWeek16_Color($this->getWeek16_Percentage());
		}
	}
	
	public function getWeek16_Percentage() {
		return $this->week16_percentage;
	}
	
	private function setWeek16_Percentage($current, $week16) {
		$this->week16_percentage = $this->calculatePercentage($current, $week16);
	}
	
	public function getWeek16_Color() {
		return $this->week16_color;
	}
	
	private function setWeek16_Color($percentage) {
		$this->week16_color = $this->calculateColor($percentage);
	}
	
	public function getWeek32() {
		return $this->week32;
	}
	
	public function setWeek32($week32) {
		if ($week32 instanceof Country) {
			$this->week32 = $this->getCountry()->getActiveTeams()-$week32->getActiveTeams();
			$this->setWeek32_Percentage($this->getCountry()->getActiveTeams(), $week32->getActiveTeams());
			$this->setWeek32_Color($this->getWeek32_Percentage());
		}
	}
	
	public function getWeek32_Percentage() {
		return $this->week32_percentage;
	}
	
	private function setWeek32_Percentage($current, $week32) {
		$this->week32_percentage = $this->calculatePercentage($current, $week32);
	}
	
	public function getWeek32_Color() {
		return $this->week32_color;
	}
	
	private function setWeek32_Color($percentage) {
		$this->week32_color = $this->calculateColor($percentage);
	}
	
	public function hasVisited() {
		return $this->visited;
	}
	
	public function setVisited($visited) {
		$this->visited = $visited;
	}
	
	public function hasHosted() {
		return $this->hosted;
	}
	
	public function setHosted($hosted) {
		$this->hosted = $hosted;
	}
	
	public function getColor() {
		return $this->color;
	}
	
	private function setColor($visited, $hosted) {
		if ($visited == true && $hosted == true) {
			$this->color = '#C0FFC0';
		}
		else if ($visited == true) {
			$this->color = '#C0C0FF';
		}
		else if ($hosted == true) {
			$this->color = '#FFFFC0';
		}
		else $this->color = '#FFFFFF';
	}
	
	private function calculateNeededDivisions($users) {
		$divisionSizes = array(8, 40, 168, 680, 2728, 10920, 19112, 35496, 51880, 84648, 117416);
		$neededDivisions = 1;
		for ($i = 0; $i <= 10; $i++) {
			if ($users > $divisionSizes[$i]) {
				$neededDivisions = $neededDivisions+1;
			}
		}
		return $neededDivisions;
	}
	
	private function calculatePercentage($current, $former) {
		if ($former > 0) {
			return round(($current-$former)/$former*100, 2);
		}
		else return null;
	}

	private function calculateColor($percentage) {
		if ($percentage == 0) {
			return '#FFFFFF';
		}
		else if ($percentage >= 100) {
			return '#00FF00';
		}
		else if ($percentage < -50) {
			return '#FF0000';
		}
		else if ($percentage < 0) {
			$tint = 255-round(-2.54*2*$percentage);
			return sprintf('#FF%02X%02X', $tint, $tint);
		}
		else if ($percentage > 0) {
			$tint = 255-round(2.54*$percentage);
			return sprintf('#%02XFF%02X', $tint, $tint);
		}
		else return '#FFFFFF';
	}
	
}

?>